#include "DbCRUDService.h"
#include "../../tools/DbCRUD/DbGet.h" // 包含修改后的 DbGet 工具类
#include <iostream>
#include <grpcpp/grpcpp.h> // For grpc::Status, grpc::StatusCode

using grpc::Status;
using grpc::StatusCode;

// --- 读取 (Read) - 单个查询 ---
Status DbCRUDService::GetUser(
    ServerContext* context,
    const GetUserRequest* request,
    GetUserResponse* response) {
    
    int32_t user_id = request->user_id();
    ::graph::UserNode user_node; // 用于接收 DbGet 返回的数据
    std::string error_message;

    // 调用修改后的 DbGet::GetUser
    // 注意：GetResult 是在 DbGet 类外部定义的枚举，直接使用 GetResult:: 枚举值
    GetResult db_result = DbGet::GetUser(user_id, user_node, error_message);

    switch (db_result) {
        case GetResult::Found: // <-- 修正：直接使用枚举名，不带 DbGet::
            // 成功找到节点
            response->set_found(true);
            response->mutable_user()->CopyFrom(user_node); // 将数据复制到响应
            std::cout << "GetUser: Successfully found user with ID: " << user_id << std::endl;
            break;

        case GetResult::NotFound: // <-- 修正
            // 成功执行查询，但未找到节点
            response->set_found(false);
            // 可以选择性地设置一个信息
            // response->set_error_message("User not found"); 
            std::cout << "GetUser: User with ID " << user_id << " not found." << std::endl;
            break;

        case GetResult::Error: // <-- 修正
            // 数据库操作失败
            response->set_found(false); // 或者不设置 found，因为有错误
            response->set_error_message(error_message);
            std::cerr << "GetUser failed for ID: " << user_id << ". Error: " << error_message << std::endl;
            // 可以根据 error_message 内容返回更具体的 gRPC 状态码
            // 例如，如果是数据库连接问题，可以返回 StatusCode::UNAVAILABLE
            // 这里为了简化，统一返回 INTERNAL
            return Status(StatusCode::INTERNAL, error_message);
            break; // break 在 return 后是多余的，但无害
    }

    // 操作成功（无论是找到、未找到还是返回了错误信息）
    return Status::OK;
}

// --- 其他 Get* 方法实现 ---
Status DbCRUDService::GetList(
    ServerContext* context,
    const GetListRequest* request,
    GetListResponse* response) {
    
    int32_t list_id = request->list_id();
    ::graph::ListNode list_node;
    std::string error_message;

    // 注意：GetResult 是在 DbGet 类外部定义的枚举
    GetResult db_result = DbGet::GetList(list_id, list_node, error_message);

    switch (db_result) {
        case GetResult::Found: // <-- 修正
            response->set_found(true);
            response->mutable_list()->CopyFrom(list_node);
            std::cout << "GetList: Successfully found list with ID: " << list_id << std::endl;
            break;

        case GetResult::NotFound: // <-- 修正
            response->set_found(false);
            std::cout << "GetList: List with ID " << list_id << " not found." << std::endl;
            break;

        case GetResult::Error: // <-- 修正
            response->set_found(false);
            response->set_error_message(error_message);
            std::cerr << "GetList failed for ID: " << list_id << ". Error: " << error_message << std::endl;
            return Status(StatusCode::INTERNAL, error_message);
            break; // break 在 return 后是多余的，但无害
    }

    return Status::OK;
}

Status DbCRUDService::GetTodo(
    ServerContext* context,
    const GetTodoRequest* request,
    GetTodoResponse* response) {
    
    int32_t todo_id = request->todo_id();
    ::graph::TodoNode todo_node;
    std::string error_message;

    // 注意：GetResult 是在 DbGet 类外部定义的枚举
    GetResult db_result = DbGet::GetTodo(todo_id, todo_node, error_message);

    switch (db_result) {
        case GetResult::Found: // <-- 修正
            response->set_found(true);
            response->mutable_todo()->CopyFrom(todo_node);
            std::cout << "GetTodo: Successfully found todo with ID: " << todo_id << std::endl;
            break;

        case GetResult::NotFound: // <-- 修正
            response->set_found(false);
            std::cout << "GetTodo: Todo with ID " << todo_id << " not found." << std::endl;
            break;

        case GetResult::Error: // <-- 修正
            response->set_found(false);
            response->set_error_message(error_message);
            std::cerr << "GetTodo failed for ID: " << todo_id << ". Error: " << error_message << std::endl;
            return Status(StatusCode::INTERNAL, error_message);
            break; // break 在 return 后是多余的，但无害
    }

    return Status::OK;
}

Status DbCRUDService::GetComment(
    ServerContext* context,
    const GetCommentRequest* request,
    GetCommentResponse* response) {
    
    int32_t comment_id = request->comment_id();
    ::graph::CommentNode comment_node;
    std::string error_message;

    // 注意：GetResult 是在 DbGet 类外部定义的枚举
    GetResult db_result = DbGet::GetComment(comment_id, comment_node, error_message);

    switch (db_result) {
        case GetResult::Found: // <-- 修正
            response->set_found(true);
            response->mutable_comment()->CopyFrom(comment_node);
            std::cout << "GetComment: Successfully found comment with ID: " << comment_id << std::endl;
            break;

        case GetResult::NotFound: // <-- 修正
            response->set_found(false);
            std::cout << "GetComment: Comment with ID " << comment_id << " not found." << std::endl;
            break;

        case GetResult::Error: // <-- 修正
            response->set_found(false);
            response->set_error_message(error_message);
            std::cerr << "GetComment failed for ID: " << comment_id << ". Error: " << error_message << std::endl;
            return Status(StatusCode::INTERNAL, error_message);
            break; // break 在 return 后是多余的，但无害
    }

    return Status::OK;
}

Status DbCRUDService::GetEntity(
    ServerContext* context,
    const GetEntityRequest* request,
    GetEntityResponse* response) {
    
    const std::string& entity_id = request->entity_id(); // 字符串 ID
    ::graph::EntityNode entity_node;
    std::string error_message;

    // 注意：GetResult 是在 DbGet 类外部定义的枚举
    GetResult db_result = DbGet::GetEntity(entity_id, entity_node, error_message);

    switch (db_result) {
        case GetResult::Found: // <-- 修正
            response->set_found(true);
            response->mutable_entity()->CopyFrom(entity_node);
            std::cout << "GetEntity: Successfully found entity with ID: " << entity_id << std::endl;
            break;

        case GetResult::NotFound: // <-- 修正
            response->set_found(false);
            std::cout << "GetEntity: Entity with ID '" << entity_id << "' not found." << std::endl;
            break;

        case GetResult::Error: // <-- 修正
            response->set_found(false);
            response->set_error_message(error_message);
            std::cerr << "GetEntity failed for ID: " << entity_id << ". Error: " << error_message << std::endl;
            return Status(StatusCode::INTERNAL, error_message);
            break; // break 在 return 后是多余的，但无害
    }

    return Status::OK;
}